//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssPurcReturnListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import com.aidong.model.wills.PssPurcReturn;
import com.aidong.model.wills.PssPurcReturnImpl;
import com.aidong.model.wills.PssPurcReturnList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssPurcReturnDao extends BaseDao<PssPurcReturn, Integer> {
    public PssPurcReturnDao() {
    }

    public void addPurcReturn(PssPurcReturn pssPurcReturn, PssPurcReturnListDao pssPurcReturnListDao, PssProductStockDao pssProductStockDao, List<PssPurcReturnList> pssPurcReturnList) {
        this.add(pssPurcReturn);
        Iterator i$ = pssPurcReturnList.iterator();

        while(i$.hasNext()) {
            PssPurcReturnList pss = (PssPurcReturnList)i$.next();
            pss.setReturnID(pssPurcReturn.getReturnID());
            pss.setAmount(Double.valueOf(pss.getPrice().doubleValue() * pss.getQuantity().doubleValue()));
            this.getSession().save(pss);
            PssProductStockPK pssProductStockPK = new PssProductStockPK();
            pssProductStockPK.setProductCode(pss.getProductCode());
            pssProductStockPK.setShopCode(pssPurcReturn.getShopCode());
            PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
            if(pssProductStock == null) {
                pssProductStock = new PssProductStock();
                pssProductStock.setProductCode(pss.getProductCode());
                pssProductStock.setShopCode(pssPurcReturn.getShopCode());
                pssProductStock.setQuantity(Double.valueOf(0.0D - pss.getQuantity().doubleValue()));
                pssProductStock.setCreateUser(pssPurcReturn.getCreateuser());
                pssProductStock.setCreateTime(new Date());
                pssProductStock.setUpdateTime(new Date());
                pssProductStockDao.add(pssProductStock);
            } else {
                pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() - pss.getQuantity().doubleValue()));
                pssProductStock.setUpdateTime(new Date());
                pssProductStockDao.update(pssProductStock);
            }
        }

    }

    public void updatePurcReturn(PssPurcReturnListDao pssPurcReturnListDao, PssProductStockDao pssProductStockDao, String shopCode, Products products) {
        Iterator i$ = products.getLstProduct().iterator();

        while(i$.hasNext()) {
            PssPopProduct pc = (PssPopProduct)i$.next();
            PssPurcReturnList pssPurcReturnList = (PssPurcReturnList)pssPurcReturnListDao.getById(pc.getId());
            double oldQty = pssPurcReturnList.getQuantity().doubleValue();
            if(Double.compare(pc.getQty().doubleValue(), pssPurcReturnList.getQuantity().doubleValue()) != 0) {
                pssPurcReturnList.setQuantity(pc.getQty());
                pssPurcReturnList.setAmount(Double.valueOf(pssPurcReturnList.getPrice().doubleValue() * pc.getQty().doubleValue()));
                pssPurcReturnListDao.update(pssPurcReturnList);
                PssProductStockPK pssProductStockPK = new PssProductStockPK();
                pssProductStockPK.setProductCode(pc.getProductCode());
                pssProductStockPK.setShopCode(shopCode);
                PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
                pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() - (pc.getQty().doubleValue() - oldQty)));
                pssProductStock.setUpdateTime(new Date());
                pssProductStockDao.update(pssProductStock);
            }
        }

    }

    public void getPurcReturnRptList(Pagination<PssPurcReturnImpl> pagination, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select pr.ShopCode, pc.defaultContract, pr.ReturnDate, pr.createuser, prl.ProviderCode, prl.ProductCode, pc.ProductName,prl.quantity,prl.amount  from PssPurcReturn pr  inner join PssPurcReturnList prl on pr.ReturnID=prl.ReturnID  left outer  join PssProductCode pc on prl.productcode=pc.ProductCode  " + (sqlw.length() > 0?"where " + sqlw:"");
        sql = sql + " order by pr.ShopCode, prl.productcode ";
        this.queryPageListBySql(PssPurcReturnImpl.class, pagination, sql, params.toArray());
    }
}
